package leetcode.median;

import leetcode.base.ListNode;

/**
 * ClassName: $142_LinkedListCycleII
 * Description:
 * date: 2021-10-21 11:23
 *
 * @author liyifan
 */
public class $142_LinkedListCycleII {
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        do {
            if (fast.next == null || fast == null)
                return null;

            fast = fast.next.next;
            slow = slow.next;
        } while (fast != slow);

        fast = head;
        while (fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }
}
